package com.haochang.chunkvod.controller.fragment.hostess;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;

import com.haochang.base.BaseActivity;
import com.haochang.base.BaseEditText;
import com.haochang.base.BaseTextView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.utils.SoftKeyboardUtils;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.model.hostess.HostessData;
import com.haochang.chunkvod.model.hostess.HostessInfo;
import com.haochang.chunkvod.view.vod.dialog.ChooseRoomDialog;
import com.haochang.chunkvod.view.vod.dialog.KeyBoardDialog;
import com.haochang.chunkvod.view.vod.timepicker.TimePicker;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;

/**
 * 咨客--预留房间--新增预留房间
 *     @author Yangjie
 *     className Fragment_add_reserve_room
 *     created at  2016/11/1  17:54
 */
public class AddReserveRoomFragment extends Fragment implements KeyBoardDialog.KeyBoardOnSureListener
{
	private Context mContext;
	private View view;// infalte的布局
	private LinearLayout myContainer;// 新建容器
	////////////////////////////////
	private LinearLayout linear_choose_reserve_date, linear_choose_reserve_room_type;//可点击的横条
	private BaseEditText et_customer_nick, et_reserver, et_reserve_number;//Android自带输入
	private BaseTextView tv_counts_of_member;//弹出数字键盘
	private BaseTextView tv_choosed_reserve_date;//显示选择的日期
	private BaseTextView tv_choosed_reserve_room_type;//显示选择的房型
	private BaseTextView tv_begin_hour, tv_begin_minute, tv_end_hour, tv_end_minute;//弹出数字键盘
	private BaseTextView tv_sure;//确定提交请求
	private BaseTextView clickedView;//记录点击弹出数字键盘的view

	private HostessData hostessData;
	private List<HostessInfo> hostessInfoList;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mContext = getActivity();
		hostessData = new HostessData((BaseActivity) mContext);
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		if (null == view)
		{
			myContainer = new LinearLayout(mContext);
			view = inflater.inflate(R.layout.fragment_add_reserve_room, container, false);
			view.setMinimumHeight(((WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getHeight());
			view.setMinimumWidth(((WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth());
			myContainer.addView(view);
		} else
		{
			myContainer.removeAllViews();
			myContainer = new LinearLayout(getActivity());
			myContainer.addView(view);
		}
		initView();
		initData();
		requestEmptyRoomList();//初始获取空房列表
		return myContainer;
	}

	private void initView()
	{
		linear_choose_reserve_date = (LinearLayout) view.findViewById(R.id.linear_choose_reserve_date);
		linear_choose_reserve_room_type = (LinearLayout) view.findViewById(R.id.linear_choose_reserve_room_type);

		et_customer_nick = (BaseEditText) view.findViewById(R.id.et_customer_nick);
		et_reserver = (BaseEditText) view.findViewById(R.id.et_reserver);
		et_reserve_number = (BaseEditText) view.findViewById(R.id.et_reserve_number);

		tv_counts_of_member = (BaseTextView) view.findViewById(R.id.tv_counts_of_member);
		tv_begin_hour = (BaseTextView) view.findViewById(R.id.tv_begin_hour);
		tv_begin_minute = (BaseTextView) view.findViewById(R.id.tv_begin_minute);
		tv_end_hour = (BaseTextView) view.findViewById(R.id.tv_end_hour);
		tv_end_minute = (BaseTextView) view.findViewById(R.id.tv_end_minute);

		tv_choosed_reserve_date = (BaseTextView) view.findViewById(R.id.tv_choosed_reserve_date);
		tv_choosed_reserve_room_type = (BaseTextView) view.findViewById(R.id.tv_choosed_reserve_room_type);

		tv_sure = (BaseTextView) view.findViewById(R.id.tv_sure);
	}

	private void initData()
	{
		linear_choose_reserve_date.setOnClickListener(onBaseClickListener);
		linear_choose_reserve_room_type.setOnClickListener(onBaseClickListener);
		tv_sure.setOnClickListener(onBaseClickListener);

		tv_counts_of_member.setOnClickListener(clickShowDigitalKeyBoard);
		tv_begin_hour.setOnClickListener(clickShowDigitalKeyBoard);
		tv_begin_minute.setOnClickListener(clickShowDigitalKeyBoard);
		tv_end_hour.setOnClickListener(clickShowDigitalKeyBoard);
		tv_end_minute.setOnClickListener(clickShowDigitalKeyBoard);

		tv_begin_hour.addTextChangedListener(textWatcher);
		tv_begin_minute.addTextChangedListener(textWatcher);
		tv_end_hour.addTextChangedListener(textWatcher);
		tv_end_minute.addTextChangedListener(textWatcher);
	}

	/**
	 * 请求新增预留房间
	 */
	private void requestAddReserveRoom()
	{
		hostessData.requestAddReserveRoom("", new HostessData.ResultResponse()
		{
			@Override
			public void onRequestSuccess()
			{
				ToastUtils.showText(mContext, "添加预留房间成功");
			}

			@Override
			public void onFail(int errorNo, String errorMsg)
			{
				ToastUtils.showText(mContext, TextUtils.isEmpty(errorMsg) ? "添加预留房间失败" : errorMsg);
			}
		});
	}

	/**
	 * 请求空房list
	 */
	private void requestEmptyRoomList()
	{
		hostessData.requestEmptyRoomList(new HostessData.ResultResponseList<HostessInfo>()
		{
			@Override
			public void onRequestSuccess(List<HostessInfo> list)
			{
				hostessInfoList = list;
			}

			@Override
			public void onFail(int errno, String errMsg)
			{
			}
		});
	}

	/**
	 * @return 请求是否合法
	 */
	private boolean isLegal()
	{
		if (TextUtils.isEmpty(tv_choosed_reserve_room_type.getText().toString()))
		{
			ToastUtils.showText(mContext, "请先选择预留房型!");
			return false;
		}
		if (TextUtils.isEmpty(tv_choosed_reserve_date.getText().toString()))
		{
			ToastUtils.showText(mContext, "请先选择预留日期!");
			return false;
		}
		if ((Integer.parseInt(tv_begin_hour.getText().toString()) > Integer.parseInt(tv_end_hour.getText().toString()))
				|| (Integer.parseInt(tv_begin_hour.getText().toString()) == Integer.parseInt(tv_end_hour.getText().toString()))
						&& Integer.parseInt(tv_begin_minute.getText().toString()) >= Integer.parseInt(tv_end_minute.getText().toString()))
		{
			ToastUtils.showText(mContext, "请先填写预留时长!");
			return false;
		}
		if (TextUtils.isEmpty(et_customer_nick.getText().toString()))
		{
			et_customer_nick.requestFocus();
			ToastUtils.showText(mContext, "请输入顾客尊称!");
			SoftKeyboardUtils.showSoftInput(mContext, et_customer_nick);
			return false;
		}
		if (TextUtils.isEmpty(tv_counts_of_member.getText().toString()))
		{
			ToastUtils.showText(mContext, "请输入人数!");
			return false;
		}
		if (TextUtils.isEmpty(et_reserver.getText().toString()))
		{
			et_reserver.requestFocus();
			ToastUtils.showText(mContext, "请输入预留人姓名!");
			SoftKeyboardUtils.showSoftInput(mContext, et_reserver);
			return false;
		}
		if (TextUtils.isEmpty(et_reserve_number.getText().toString()))
		{
			et_reserve_number.requestFocus();
			ToastUtils.showText(mContext, "请输入预留号!");
			SoftKeyboardUtils.showSoftInput(mContext, et_reserve_number);
			return false;
		}
		return true;
	}

	OnBaseClickListener onBaseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			switch (v.getId())
			{
			case R.id.linear_choose_reserve_room_type:
				//选择房型
				if (null == hostessInfoList)
				{
					ToastUtils.showText(mContext, "正在获取房间列表...");
					requestEmptyRoomList();
					return;
				}
				new ChooseRoomDialog.Builder(mContext).setTitle("选择房间").setSingleChooseItems(hostessInfoList, new ChooseRoomDialog.OnSingleChoosedListener()
				{
					@Override
					public void onSingleChoosed(HostessInfo info)
					{
						tv_choosed_reserve_room_type.setText(info.getRoomNum());
					}
				}).showDialog(8,20);
				break;
			case R.id.linear_choose_reserve_date:
				//选择日期
				new TimePicker(mContext, new TimePicker.ResultCallBack()
				{
					@Override
					public void choosed(String year, String month, String day)
					{
						tv_choosed_reserve_date.setText(String.valueOf(year + "/" + month + "/" + day));
					}
				}).show();
				break;
			case R.id.tv_sure:
				//确定
				if (isLegal())
					requestAddReserveRoom();
				break;
			default:
				break;
			}
		}
	};
	OnBaseClickListener clickShowDigitalKeyBoard = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			clickedView = (BaseTextView) v;
			KeyBoardDialog keyBoardDialog = new KeyBoardDialog(mContext);
			keyBoardDialog.setKeyBoardOnSureListener(AddReserveRoomFragment.this);
			keyBoardDialog.show();
		}
	};
	TextWatcher textWatcher = new TextWatcher()
	{
		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after)
		{

		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count)
		{
			if ((clickedView == tv_begin_hour || clickedView == tv_end_hour) && Integer.parseInt(clickedView.getText().toString().trim()) > 23)
			{
				clickedView.setText(String.valueOf(23));
			}
			if ((clickedView == tv_begin_minute || clickedView == tv_end_minute) && Integer.parseInt(clickedView.getText().toString().trim()) > 59)
			{
				clickedView.setText(String.valueOf(59));
			}
			if ((clickedView == tv_end_hour || clickedView == tv_end_minute)
					&& (Integer.parseInt(tv_begin_hour.getText().toString()) > Integer.parseInt(tv_end_hour.getText().toString()))
					|| (Integer.parseInt(tv_begin_hour.getText().toString()) == Integer.parseInt(tv_end_hour.getText().toString()))
							&& Integer.parseInt(tv_begin_minute.getText().toString()) >= Integer.parseInt(tv_end_minute.getText().toString()))
			{
				ToastUtils.showText(mContext, "结束时间小于开始时间");
			}
		}

		@Override
		public void afterTextChanged(Editable s)
		{

		}
	};

	@Override
	public void setValue(String value)
	{
		DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getInstance();
		decimalFormat.applyPattern("00");
		try
		{
			if (value.contains("."))
				ToastUtils.showText(mContext, R.string.error_number);
			else if (Integer.parseInt(value) <= 0)
				ToastUtils.showText(mContext, R.string.error_number);
			else
				clickedView.setText(clickedView == tv_counts_of_member ? value : decimalFormat.format(Long.parseLong(value)));
		} catch (Exception e)
		{
			ToastUtils.showText(mContext, getResources().getString(R.string.error_max_number));
		}
	}
}
